﻿using FasterWPF;
using FasterWPF.Generators;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FasterWPFDemo
{
    //Wrap Panel
    public class TabItem02 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        public TabItem02()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            // this.Width = 850D;
            // this.Height = 750D;

            this.BeginSettings<UserControl>()
                .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                .SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3)).EndSettings();
        }

        public void LoadUserControl()
        { 
            string skpbGuid1 = Guid.NewGuid().ToString();
            string skpbGuid2 = Guid.NewGuid().ToString();
            string skpbGuid3 = Guid.NewGuid().ToString();
            string drpGuid = Guid.NewGuid().ToString();
            string wrapGuid = Guid.NewGuid().ToString();
            string wrapGuid2 = Guid.NewGuid().ToString();
            string wrapGuid3 = Guid.NewGuid().ToString();
            string wrapGuid4 = Guid.NewGuid().ToString();
            string wrapGuid5 = Guid.NewGuid().ToString();
            string wrapGuid6 = Guid.NewGuid().ToString();
            string wrapGuid7 = Guid.NewGuid().ToString();

            ScrollViewer scrollViewer4WrapPanel = null;
            scrollViewer4WrapPanel = new ScrollViewer();

            StackPanel skpb2 = new StackPanel();
            skpb2.Initialize(double.NaN, double.NaN, ContainerType.HorizontalPanel, Orientation.Vertical);
            skpb2.SetBackgroundColor(Brushes.Green);
            skpb2.BeginSettings<StackPanel>()
                .SetTextPadding<StackPanel>(0, 0, new Thickness(13))
                .SetTextColorToCol<StackPanel>(0, 0, 3, Brushes.White)
                .EndSettings<StackPanel>();

            skpb2.BeginComposite<StackPanel>(skpbGuid1)
                .AddText<StackPanel>(0, 0, "Caribbean Islands")
                .EndComposite<StackPanel, StackPanelArgs>(null);

            skpb2.GetTextBlock<StackPanel>(skpbGuid1, 0, 0).FontWeight = FontWeights.Bold;
            skpb2.GetTextBlock<StackPanel>(skpbGuid1, 0, 0).FontSize = 20D;
            skpb2.GetTextBlock<StackPanel>(skpbGuid1, 0, 0).TextDecorations = TextDecorations.Underline;

            skpb2.BeginComposite<StackPanel>(skpbGuid2)
                .AddText<StackPanel>(0, 0, "Grand Cayman")
                .EndComposite<StackPanel, StackPanelArgs>(null);

            skpb2.BeginComposite<StackPanel>(skpbGuid3)
                .AddText<StackPanel>(0, 0, "Puerto Rico")
                .EndComposite<StackPanel, StackPanelArgs>(null);
            
            WrapPanel wrap1 = new WrapPanel();
            GroupBox gpParent = new GroupBox();
            gpParent.Name = "myGroupBox";
            TextBlock tlbh2 = new TextBlock(new Run("Header"));
            tlbh2.Background = Brushes.WhiteSmoke;
            FontSettings fsh1 = FontExt.CreateFont("Arial", 9D, FontWeights.Bold, FontStyles.Normal);
            FontExt.ApplyFontSettings(fsh1, tlbh2);
            gpParent.Header = tlbh2;
            gpParent.Initialize(double.NaN, double.NaN, ContainerType.DockPanel);
            //gpParent.SetCompositeWrapPanelSettings<GroupBox>(100D, 100D, Orientation.Horizontal);
            gpParent.SetCompositeDockPanelLastChildFill<GroupBox>(true);

            gpParent.BeginSettings<GroupBox>()
                .SetTextWrapping<GroupBox>((int)Dock.Right, 1, TextWrapping.Wrap)
                .Set<TextBlock, GroupBox>((int)Dock.Right, 1, "Width", 50D)
                .SetFont<GroupBox>((int)Dock.Right, 1, "Calibri", 14, FontWeights.Bold, FontStyles.Oblique)
                .EndSettings<GroupBox>();

            gpParent.BeginComposite(drpGuid)
                .AddText<GroupBox>((int)Dock.Right, 1, "Hi, this is text to the right of the image.")
                .AddImage((int)Dock.Right, 0, @"pack://siteoforigin:,,,/Images/sunset.bmp", UriKind.Absolute, 150D)
                .EndComposite<GroupBox, GroupBoxArgs>(null);

            wrap1.Initialize(300, double.NaN, ContainerType.DockPanel, double.NaN, double.NaN, Orientation.Horizontal, FlowDirection.LeftToRight);
            wrap1.SetCompositeDockPanelLastChildFill<WrapPanel>(true);

            wrap1.BeginSettings<WrapPanel>()
                .SetItemBorderSettings<WrapPanel>(double.NaN, double.NaN, Brushes.Gold, new Thickness(2))
                .EndSettings<WrapPanel>();

            wrap1.BeginComposite<WrapPanel>(wrapGuid)
                .AddExisting<StackPanel, WrapPanel>(0, 0, skpb2)
                .AddText<WrapPanel>(0, 1, "Hello World 1")
                .EndComposite<WrapPanel, WrapPanelArgs>(null);

            wrap1.BeginComposite<WrapPanel>(wrapGuid2)
                .AddText<WrapPanel>((int)Dock.Bottom, 0, "This is text beneath the image.")
                .AddExisting<GroupBox, WrapPanel>((int)Dock.Bottom, 1, gpParent)
                .EndComposite<WrapPanel, WrapPanelArgs>(null);

            wrap1.BeginComposite<WrapPanel>(wrapGuid3)
                .AddText<WrapPanel>(0, 0, "Hello World 3")
                .EndComposite<WrapPanel, WrapPanelArgs>(null);

            wrap1.BeginComposite<WrapPanel>(wrapGuid4)
                 .AddText<WrapPanel>(0, 0, "Hello World 4")
                 .EndComposite<WrapPanel, WrapPanelArgs>(null);

            wrap1.BeginComposite<WrapPanel>(wrapGuid5)
                 .AddText<WrapPanel>(0, 0, "Hello World 5")
                 .EndComposite<WrapPanel, WrapPanelArgs>(null);

            wrap1.BeginComposite<WrapPanel>(wrapGuid6)
                 .AddText<WrapPanel>(0, 0, "Hello World 6")
                 .EndComposite<WrapPanel, WrapPanelArgs>(null);

            wrap1.BeginComposite<WrapPanel>(wrapGuid7)
                 .AddText<WrapPanel>(0, 0, "Hello World 7")
                 .EndComposite<WrapPanel, WrapPanelArgs>(null);

            //Generate 5 Composites with Labels inside them
            int indx=0;
            Label prototypeLabel = new Label() { Height = 43D, Width = 43D, Background = Brushes.Goldenrod };
            Action<int, Label> labelConstructor = (f, lbl) => { indx++; if (f == 1) { lbl.Background = Brushes.Green; } lbl.Foreground = Brushes.White; lbl.Content = indx.ToString(); };
            Generators.AddComposites<Label>(wrap1, 0, 0, 0, 4, prototypeLabel, labelConstructor, Orientation.Horizontal);

            //Add a Composite with 3 Child Labels
            int indx2 = 0;
            Label prototypeLabel2 = new Label() { Height = 43D, Width = 43D, Background = Brushes.Aqua};
            Action<int, Label> labelConstructor2 = (f, lbl) => { indx2++; if (f == 1) { lbl.Background = Brushes.Green; } lbl.Foreground = Brushes.White; lbl.Content = "0"; }; // indx2.ToString(); 
            Generators.AddChildrenInColumnRange(wrap1, 0, 0, 2, prototypeLabel2, labelConstructor2, null);

            string scrollWrapGuid = Guid.NewGuid().ToString();
            scrollViewer4WrapPanel.Initialize(350D, 350D, ContainerType.HorizontalPanel, new Thickness(3, 2, 3, 13), null, null);
            scrollViewer4WrapPanel.ApplyScrollViewerStrategy("LawnGreen", "Green", "ForestGreen");

            scrollViewer4WrapPanel.GetVerticalScrollBar<ScrollViewer>().Visibility = Visibility.Visible;

            scrollViewer4WrapPanel.BeginSettings<ScrollViewer>().SetItemBorderColorAndThickness(Brushes.LightGray, new Thickness(3)).EndSettings<ScrollViewer>();
            scrollViewer4WrapPanel.BeginComposite<ScrollViewer>(scrollWrapGuid)
                .AddExisting<WrapPanel, ScrollViewer>(0, 0, wrap1)
                .EndComposite<ScrollViewer, ScrollViewerArgs>(null);
                                   
            int counter = 7;
            var myNewFlatButton = FlatButtonHelpers.NewFlatButton(175D, 50D, "Add Item to Wrap Panel", VerticalAlignment.Center, HorizontalAlignment.Center, Brushes.Black, new Thickness(0), Brushes.Khaki, new CornerRadius(10), new Thickness(0), new Thickness(1), Brushes.Goldenrod,
                (o, e) =>
                {
                    counter++; 
                    System.Windows.MessageBox.Show("Adding Hello World " + counter.ToString());                    
                    wrap1.BeginComposite<WrapPanel>()
                         .AddText<WrapPanel>(0, 0, "Hello World" + counter.ToString())
                         .EndComposite<WrapPanel, WrapPanelArgs>(null);
                });

            var myNewFlatButtonSortAsc = FlatButtonHelpers.NewFlatButton(175D, 50D, "Sort Wrap Panel Asc", VerticalAlignment.Center, HorizontalAlignment.Center, Brushes.Black, new Thickness(0), Brushes.Khaki, new CornerRadius(10), new Thickness(0), new Thickness(1), Brushes.Goldenrod,
                (o, e) =>
                {
                    //wrap1.SortByText<WrapPanel>(0, 0, CommonExt.SortOrder.Asc, CommonExt.SortControlType.Label);
                    WrapPanelSort wrp = new WrapPanelSort(CommonExt.SortDataType.Integer, CommonExt.SortDataType.String);
                    wrap1.SortByText1AndThenBy2(wrp, 0, 0, 0, 1, CommonExt.SortOrder.Asc, CommonExt.SortOrder.Asc, CommonExt.SortControlType.Label);
                });

            var myNewFlatButtonSortDesc = FlatButtonHelpers.NewFlatButton(175D, 50D, "Sort Wrap Panel Desc", VerticalAlignment.Center, HorizontalAlignment.Center, Brushes.Black, new Thickness(0), Brushes.Khaki, new CornerRadius(10), new Thickness(0), new Thickness(1), Brushes.Goldenrod,
                (o, e) =>
                {
                    WrapPanelSort wrp = new WrapPanelSort(CommonExt.SortDataType.Integer);
                    wrap1.SortByText<WrapPanel>(wrp, 0, 0, CommonExt.SortOrder.Desc, CommonExt.SortControlType.Label);
                });

            StackPanel stkcpanel1 = new StackPanel();
            string stkcpanel1Guid = Guid.NewGuid().ToString();
            stkcpanel1.Initialize(ContainerType.HorizontalPanel, Orientation.Horizontal, 1, 1);
            stkcpanel1.BeginSettings<StackPanel>().Set<Border, StackPanel>(0, 2, "Margin", new Thickness(10)).EndSettings<StackPanel>();

            stkcpanel1.BeginComposite<StackPanel>(stkcpanel1Guid)
                      .AddExisting<ScrollViewer, StackPanel>(0, 0, scrollViewer4WrapPanel)
                      .AddExisting<Border, StackPanel>(0, 2, myNewFlatButton)
                      .AddExisting<Border, StackPanel>(0, 3, myNewFlatButtonSortAsc)
                      .AddExisting<Border, StackPanel>(0, 4, myNewFlatButtonSortDesc)  
                      .EndComposite<StackPanel, StackPanelArgs>(null);

            this.BeginComposite<UserControl>(_userCtlGuid)
                      .AddLabel<UserControl>(0, 0, "Tab Item 2 -  Wrap Panel Example", Brushes.Gold)
                      .AddExisting<StackPanel, UserControl>(0, 1, stkcpanel1)
                      .AddLabel<UserControl>(0, 2, "", Brushes.DarkGoldenrod)
                      .AddLabel<UserControl>(0, 3, "", Brushes.LightGoldenrodYellow)
                      .AddFlatButton<UserControl>(0, 4, 120D, 120D,
                "Remove this\n UserControl!", Brushes.GhostWhite, new Thickness(2), Brushes.Gold, new CornerRadius(0), new Thickness(2), new Thickness(3), Brushes.Orange,
                (o, ev) =>
                {
                    //Retrieve Action from Global Store on Application object
                    GlobalStoreService.GetFromGlobalStore<Action<object, RoutedEventArgs>>(GlobalStoreService.TabControlGuids.Two, "TabItem2RemoveUserControl")(o, (RoutedEventArgs)ev);

                }).EndComposite<UserControl, UserControlArgs>(null);
        }

    }
}
